﻿/// <reference path="tfcore.js" />
/// <reference path="appcommon.js" />
// #pragma NoCompStart
/*
tfpopup.js  2.0a
handles modal dialog boxes, etc
*/
// #pragma NoCompEnd
// ModalDlg code

//NameSpaces defined
if (typeof TF === "undefined") { TF = {}; }
if (typeof _TF.Controls === "undefined") { _TF.Controls = {}; }
_TF.Controls.Dialog = {};  // _TF.Controls namespace


    var returnFunc = { pbContentPage: 1, pbPrevDlg: 2 };  // cause postback of IFrame, or Postback of Previous Modal Dialog, OR send a javascript function to run after button clicked.
    getWidth = function(width) { var pWidth; if (width == undefined) { width = '85%'; } if (isNaN(width)) { if (width.search(/\%/) != -1) { pWidth = getViewPortWidth() * (width.replace("\%", "") / 100); } } else { pWidth = width; } return pWidth; };
    getHeight = function(height) { var pHeight; if (height == undefined) { height = '90%'; } if (isNaN(height)) { if (height.search(/\%/) != -1) { pHeight = getViewPortHeight() * (height.replace("\%", "") / 100); } } else { pHeight = height; } return pHeight; };
    getScrollTop = function() { if (top.pageYOffset) { return top.pageYOffset; } else if (top.document.documentElement && top.document.documentElement.scrollTop) { return top.document.documentElement.scrollTop; } else if (top.document.body) { return top.document.body.scrollTop; } };


    _TF.Controls.Dialog.TabIndexes = [];
    _TF.Controls.Dialog.Iteration = -1;
    _TF.Controls.Dialog.DefaultPage = "about:blank";

    // Pre-defined list of tags we want to disable/enable tabbing into
    _TF.Controls.Dialog.TabbableTags = ["A", "BUTTON", "TEXTAREA", "INPUT", "IFRAME"];
    _TF.Controls.Dialog.disableTabIndexes = function() { if (document.all) { var i = 0; for (var j = 0; j < _TF.Controls.Dialog.TabbableTags.length; j++) { var tagElements = document.getElementsByTagName(_TF.Controls.Dialog.TabbableTags[j]); for (var k = 0; k < tagElements.length; k++) { _TF.Controls.Dialog.TabIndexes[i] = tagElements[k].tabIndex; tagElements[k].tabIndex = "-1"; i++; } } } };
    _TF.Controls.Dialog.restoreTabIndexes = function() { if (document.all) { var i = 0; for (var j = 0; j < _TF.Controls.Dialog.TabbableTags.length; j++) { var tagElements = document.getElementsByTagName(_TF.Controls.Dialog.TabbableTags[j]); for (var k = 0; k < tagElements.length; k++) { tagElements[k].tabIndex = _TF.Controls.Dialog.TabIndexes[i]; tagElements[k].tabEnabled = true; i++; } } } };



    _TF.Controls.Dialog.Create = function(options) {
        /// <summary>Instantiates a new ModalDlg as an alert an iframe or a MessageBox</summary>
        /// <param name="options" type="object">
        ///  Options = {
        ///    id: '',
        ///    type: [ iframe=1, html=2, alert=3, MsgBox=4, csMsgBox=5 ],
        ///    width: 600,
        ///    height: 600,
        ///    loc: '[CEN, N, NE, NW, W, SW, S, SE, E]',
        ///    title: 'Title of Dialog',
        ///    alertText: 'Alert Text',
        ///    alertType: [None=0,Error=1, Warning=2, Information=3],
        ///    alertTask: 'current task',
        ///    msgboxType: [None=0,ok=1, yes=2, yesno=3, yesnocancel=4],
        ///    msgboxQuestion: '',
        ///    URL: '',
        ///    HTML: '',
        ///    returnFocusID: '',
        ///    returnFunc: '',
        ///    showClose: false,
        ///    autoClose: -1,
        ///    modal: false
        ///}
        /// </param>
        /// <param name="height">width of the dialog</param>
        /// <param name="loc">use the locTypes to see what is available (locTypes.N = top of screen)</param>
        /// <returns>instance of the messagebox</returns>

        //Set up default options:
        var defaultOptions = {
            id: '',
            type: _TF.APP.Enum.contentTypes.html,
            width: 600,
            height: 600,
            loc: 'CEN',
            title: 'Dialog',
            alertText: _TF.APP.Global.g_jsErrorTxt,
            alertType: 0,
            alertTask: 'unknown',
            msgboxType: 0,
            msgboxQuestion: '',
            URL: '',
            HTML: '',
            returnFocusID: '',
            returnFunc: '',
            showClose: false,
            autoClose: -1,
            modal: false
        };
        options = options || {};
        for (var name in defaultOptions) {
            if (!options.hasOwnProperty(name)) {
                options[name] = defaultOptions[name];
            }
        }

        var _me = this; // Instantiated Object = "_me"
        _TF.Controls.Dialog.Iteration++;
        _me.iter = _TF.Controls.Dialog.Iteration;
        _me.id = options.id;
        _me.contentType = options.type;
        _me.width = options.width;
        _me.msgboxQuestion = options.msgboxQuestion;
        _me.msgboxType = options.msgboxType;
        _me.height = options.height;
        _me.loc = options.loc;
        _me.title = options.title;
        _me.blnClose = options.showClose;
        _me.alertText = options.alertText;
        _me.alertType = options.alertType;
        _me.autoClose = options.autoClose;
        _me.setFocusId = options.returnFocusID;
        _me.callBackFunc = options.returnFunc;
        _me.myHTML = options.HTML;
        _me.myURL = options.URL;
        _me.sTask = options.alertTask;
        _me.isModal = options.modal;

        var strType = "mdl";
        if (_me.contentType != _TF.APP.Enum.contentTypes.iframe) {
            strType = "alt";
        }

        _me.tBody = document.getElementById(strType + 'Dialog' + _me.iter);
        if (_me.tBody == null) {
            _me.tBody = document.createElement("div");
            _me.tBody.id = strType + "Dialog" + _me.iter;
            document.body.appendChild(_me.tBody);
        }
        _me.Mask = document.createElement("div");
        _me.Mask.id = strType + "DlgMask_" + _me.iter;
        _me.Mask.className = "ModalDlgMask";
        _me.Con = document.createElement("div");
        _me.Con.id = strType + "DlgContainer_" + _me.iter;
        _me.Con.className = "ModalDlgContainer";

        _me.MDI = document.createElement("div");
        _me.MDTB = document.createElement("div");
        _me.MDT = document.createElement("div");
        _me.MDC = document.createElement("div");
        _me.HFLD = document.createElement("INPUT");
        _me.Content = null;
        _me.arrTemp = [];


        // private functions
        _me.setTask = function(sTask) { _me.sTask = sTask; };
        _me.setType = function(contentType) { _me.contentType = contentType; };
        _me.setURL = function(sUrl) { _me.myURL = sUrl; };
        _me.setCallBackFunc = function(callBackFunc) { _me.callBackFunc = callBackFunc; };
        _me.setFocusOnReturn = function(elId) { _me.setFocusId = elId; };
        _me.setHTML = function(newHTML) { _me.myHTML = newHTML; };
        _me.setModal = function(sModal) { _me.isModal = sModal; };
        _me.setAlertType = function(aType) { _me.alertType = aType; };
        _me.setTitle = function(sTitle) { _me.title = sTitle; };
        _me.setLocation = function(locType) { _me.loc = locType; };
        _me.setAutoClose = function(sSeconds) { _me.autoClose = sSeconds * 1000; };

        _me.init = function() {
            var arrTemp = [];
            _me.MDI.id = "MDI" + _me.iter;
            _me.MDTB.id = "MDTB" + _me.iter;
            _me.MDT.id = "MDT" + _me.iter;
            _me.MDC.id = "MDI" + _me.iter;
            _me.HFLD.id = "HFLD" + _me.iter;
            _me.HFLD.setAttribute("type", "button");

            var cImg = document.createElement("img");
            cImg.id = "popCloseBox_" + _me.iter;
            var image;
            switch (_me.contentType) {
                case _TF.APP.Enum.contentTypes.MsgBox: case _TF.APP.Enum.contentTypes.csMsgBox:
                    image = edIs;
                    var cs = _me.contentType == _TF.APP.Enum.contentTypes.csMsgBox ? true : false;
                    var onClose = "javascript:_RaiseEvent('" + _me.id + "','OnClose','~t');";
                    var strButtons = '';
                    if (_me.id == '') { _me.id = 'msgBoxValue'; }

                    if (_me.callBackFunc == returnFunc.pbContentPage) {
                        onClose += "top.frames['ifMainContent'].__doPostBack('" + _me.id + "','~t');";
                    }
                    else if (_me.callBackFunc == returnFunc.pbPrevDlg) {
                        onClose += "javascript:_RaiseEvent('" + _me.id + "','OnClose','~t');postBackbPrevDlg('" + _me.id + "','~t');";
                    }

                    _me.returnFunc = ""; // clear out return function as we already raisevent!
                    onClose += "_TF.APP.Global.P_MsgBox.hide(" + _me.contentType + ");";

                    switch (_me.msgboxType) {
                        case _TF.APP.Enum.msgboxType.ok:
                            image = edIs;

                            strButtons += "<td style='width:50%;'></td>";
                            strButtons += "<td></td>";
                            strButtons += "<td><input id='s1' style='WIDTH: 50px; CURSOR: hand' onclick=\"" + onClose.replaceAll("~t", "ok") + "\" type=button value=OK name=MessageBoxIDYes1 /></td>";
                            strButtons += "<td></td>";
                            strButtons += "<td style='width:50%;'>&nbsp;</td>";
                            break;
                        case _TF.APP.Enum.msgboxType.yes:
                            image = edIs;
                            strButtons += "<td style='width:50%;'></td>";
                            strButtons += "<td></td>";
                            strButtons += "<td><input id='s1' style='WIDTH: 50px; CURSOR: hand' onclick=\"" + onClose.replaceAll("~t", "yes") + "\" type=button value=OK name=MessageBoxIDYes1 /></td>";
                            strButtons += "<td></td>";
                            strButtons += "<td style='width:50%;'></td>";
                            break;
                        case _TF.APP.Enum.msgboxType.yesno:
                            image = edIs;
                            strButtons += "<td style='width:50%;'></td>";
                            strButtons += "<td><input id='s1' style='WIDTH: 50px; CURSOR: hand' onclick=\"" + onClose.replaceAll("~t", "yes") + "\" type=button value=Yes name=MessageBoxIDYes1 /></td></td>";
                            strButtons += "<td>&nbsp;&nbsp;&nbsp;</td>";
                            strButtons += "<td><input id='s2' style='WIDTH: 50px; CURSOR: hand' onclick=\"" + onClose.replaceAll("~t", "no") + "\" type=button value=No name=MessageBoxIDNo1 /></td></td>";
                            strButtons += "<td style='width:50%;'></td>";
                            break;
                        case _TF.APP.Enum.msgboxType.yesnocancel:
                            image = edIs;
                            strButtons += "<td style='width:50%;'></td>";
                            strButtons += "<td><input id='s1' style='WIDTH: 50px; CURSOR: hand' onclick=\"" + onClose.replaceAll("~t", "yes") + "\" type=button value=Yes name=MessageBoxIDYes1 /></td></td>";
                            strButtons += "<td><input id='s2' style='WIDTH: 50px; CURSOR: hand' onclick=\"" + onClose.replaceAll("~t", "no") + "\" type=button value=No name=MessageBoxIDNo1 /></td></td>";
                            strButtons += "<td><input id='s3' style='WIDTH: 50px; CURSOR: hand' onclick=\"" + onClose.replaceAll("~t", "cancel") + "\" type=button value=Cancel name=MessageBoxIDCancel1 /></td></td>";
                            strButtons += "<td style='width:50%;'></td>";
                            break;
                    }
                    cImg.setAttribute("src", edXs);
                    _me.Content = document.createElement("div");
                    _me.MDI.className = "NotifyDlgInner";
                    _me.MDTB.className = "NotifyDlgTitleBar";
                    _me.MDT.className = "NotifyDlgTitle";
                    _me.MDT.innerHTML = _me.title;
                    _me.MDC.className = "NotifyDlgControls";
                    _me.Content.setAttribute("style", "overflow:auto;");
                    var strHTML = "<table style='width: 100%;text-align:center;background-color: lightgrey;'>";
                    strHTML += "<tr><td ><img style='border: black 1px solid; height:25px' src='" + image + "' alt='' /></td>";
                    strHTML += "<td style='text-align:left;padding:5px;' colspan='4'>" + _me.msgboxQuestion + "</td></tr><tr>";
                    strHTML += strButtons;
                    strHTML += "</tr></table></div></div>";
                    _me.Content.innerHTML = strHTML;
                    _me.setModal(true);
                    break;

                case _TF.APP.Enum.contentTypes.html:
                    image = edIs;
                    switch (_me.alertType) {
                        case _TF.APP.Enum.alertTypes.Error:
                            _me.autoClose = -1;
                            //edNLog.error(_TF.APP.Global.g_jsErrorTxt, _me.sTask);
                            image = edEs;
                            break;
                        case _TF.APP.Enum.alertTypes.Warning:
                            //edNLog.warning(_TF.APP.Global.g_jsErrorTxt, _me.sTask);
                            image = edWs;
                            break;
                        case _TF.APP.Enum.alertTypes.Information:
                            //edNLog.info(_TF.APP.Global.g_jsErrorTxt, _me.sTask);
                            image = edIs;
                            break;
                    }
                    cImg.setAttribute("src", edXs);
                    _me.Content = document.createElement("div");
                    _me.MDI.className = "NotifyDlgInner";
                    _me.MDTB.className = "NotifyDlgTitleBar";
                    _me.MDT.className = "NotifyDlgTitle";
                    _me.MDT.innerHTML = _me.title;
                    _me.MDC.className = "NotifyDlgControls";
                    _me.Content.setAttribute("style", "overflow:auto;");
                    _me.Content.innerHTML = "<table style='width:95%'><tr><td align='center'><img src='" + image + "' alt=''></img></td><td>" + _me.errText + "</td></tr></table></div></div>";
                    break;
                case _TF.APP.Enum.contentTypes.alert:
                    image = edIs;
                    switch (_me.alertType) {
                        case _TF.APP.Enum.alertTypes.Error:
                            _me.autoClose = -1;
                            //edNLog.error(_TF.APP.Global.g_jsErrorTxt, _me.sTask);
                            image = edEs;
                            break;
                        case _TF.APP.Enum.alertTypes.Warning:
                            //edNLog.warning(_TF.APP.Global.g_jsErrorTxt, _me.sTask);
                            image = edWs;
                            break;
                        case _TF.APP.Enum.alertTypes.Information:
                            //edNLog.info(_TF.APP.Global.g_jsErrorTxt, _me.sTask);
                            image = edIs;
                            break;
                    }
                    cImg.setAttribute("src", edXs);
                    _me.Content = document.createElement("div");
                    _me.MDI.className = "NotifyDlgInner";
                    _me.MDTB.className = "NotifyDlgTitleBar";
                    _me.MDT.className = "NotifyDlgTitle";
                    _me.MDT.innerHTML = _me.title;
                    _me.MDC.className = "NotifyDlgControls";
                    _me.Content.setAttribute("style", "overflow:auto;");
                    _me.Content.innerHTML = "<table style='width:95%'><tr><td align='center'><img src='" + image + "' alt=''></img></td><td>" + _me.alertText + "</td></tr></table></div></div>";
                    break;
                case _TF.APP.Enum.contentTypes.iframe:
                    _me.MDI.className = "ModalDlgInner";
                    _me.MDTB.className = "ModalDlgTitleBar";
                    _me.MDT.className = "ModalDlgTitle";
                    _me.MDT.innerHTML = "<img src='" + gsfICON + "' alt=''></img>&nbsp;&nbsp;" + _me.title;
                    _me.MDC.className = "ModalDlgControls";
                    cImg.setAttribute("src", edX);
                    _me.Content = document.createElement("iframe");
                    _me.Content.setAttribute("src", _me.myURL);
                    _me.Content.setAttribute("style", "width:100%;height:100%;background-color:transparent;");
                    _me.Content.setAttribute("scrolling", "auto");
                    _me.Content.setAttribute("frameborder", "0");
                    _me.Content.setAttribute("allowtransparency", "true");
                    _me.Content.className = "ModalDlgFrame";
                    if (_me.id != '') {
                        _me.Content.id = _me.id;
                        _me.Content.setAttribute("name", _me.id);
                    }
                    else {
                        _me.Content.id = strType + "DlgFrame_" + _me.iter;
                        _me.Content.setAttribute("name", strType + "DlgFrame_" + _me.iter);
                    }

                    _me.Content.setAttribute("width", "100%");
                    _me.Content.setAttribute("height", "100%");
                    break;
                default: break;
            }

            _me.MDC.appendChild(cImg);
            _me.MDTB.appendChild(_me.MDT);
            _me.MDTB.appendChild(_me.MDC);
            _me.MDI.appendChild(_me.MDTB);
            _me.MDI.appendChild(_me.Content);
            _me.Con.appendChild(_me.MDI);
            _me.tBody.appendChild(_me.Mask);
            _me.tBody.appendChild(_me.Con);

            _TF.Element.EnableDrag(_me.Con.id);
            //shortcut.add("esc", _me.hide, {}, _me, true);
            //specific to GSF
            //shortcut.add("esc", function() { _me.hide(_me.contentType); }, { target: _me.tBody }, _me, true);
            _AddEvent(_me.MDC, "click", function() { _me.hide(_me.contentType); });
        };

        _me.show = function show() {
            var pWidth = getWidth(_me.width);
            var pHeight = getHeight(_me.height) - 30;

            _me.init();
            var t = getLocationPoint(pWidth, pHeight, _me.loc);
            _me.Con.style.left = t.x;
            _me.Con.style.top = t.y;
            if (_me.alertType > 2) { _me.autoClose = 4000; }

            if (_me.isModal) {
                //disableTabIndexes();
                _me.Mask.style.height = getViewPortHeight();
                _me.Mask.style.width = getViewPortWidth();
                _me.Mask.style.zIndex = 800 + (_me.iter * 3);
                _me.Mask.style.display = "block";
            }

            var titleBarHeight = parseInt(_me.MDTB.offsetHeight, 10);
            _me.Con.style.width = pWidth + "px";
            _me.Con.style.height = (pHeight + titleBarHeight) + "px";
            _me.Con.style.zIndex = 801 + (_me.iter * 3);
            _me.Con.style.display = "block";
            if (_me.autoClose > 0) { setTimeout(_me.hide, _me.autoClose); }

        };      // end show

        _me.destroy = function() {
            _RemoveEvent(_me.MDC, "click", function() { _me.hide(_me.contentType); });
            _TF.Element.EnableDrag(_me.Con.id, '', '', '', false);
            _TF.Element.SafelyRemoveNode(_me.tBody); // Remove ALL children elements first to avoid memory leaks
            _TF.Controls.Dialog.Iteration--;
            if (_TF.Controls.Dialog.Iteration == -1) _TF.Controls.Dialog.Iteration = 0;
        };

        _me.hide = function(iType) {
            if (_me != null) {
                if (_me.contentType == iType) {
                    _me.Con.style.display = 'none';
                    var fn = "";
                    if (_me.callBackFunc != null) { fn = _me.callBackFunc; }
                    if (_me.setFocusId != null && _me.setFocusId.length > 0) { fn += ";setFocus(" + _me.setFocusId + ");"; }
                    if (fn.length > 0) { setTimeout(fn, 10); }
                    if (_me.isModal) {
                        //    restoreTabIndexes();
                        if (_me.Mask != undefined) { _me.Mask.style.display = 'none'; }
                    }
                    setTimeout(_me.destroy, 1000);
                }
            }
        };
    }


    function getLocationPoint(width, height, loc) {
        var theBody = document.getElementsByTagName("BODY")[0];
        var scTop = parseInt(getScrollTop(), 10);
        var scLeft = parseInt(theBody.scrollLeft, 10);
        var titleBarHeight = 0; // parseInt(_me.MDTB.offsetHeight, 10);
        var fullHeight = getViewPortHeight();
        var fullWidth = getViewPortWidth();
        var x, y;
        switch (loc) {
            case _TF.APP.Enum.locTypes.NW:
                x = "2px";
                y = "2px";
                break;
            case _TF.APP.Enum.locTypes.N:
                x = (scLeft + ((fullWidth - width) / 2)) + (10) + "px";
                y = "2px";
                break;
            case _TF.APP.Enum.locTypes.NE:
                x = (getViewPortWidth() - width) + "px";
                y = "2px";
                break;
            case _TF.APP.Enum.locTypes.E:
                x = (getViewPortWidth() - width) + "px";
                y = (scTop + ((fullHeight - (height + titleBarHeight)) / 2)) + (10) + "px"
                break;
            case _TF.APP.Enum.locTypes.SE:
                break;
            case _TF.APP.Enum.locTypes.S:
                break;
            case _TF.APP.Enum.locTypes.SW:
                break;
            case _TF.APP.Enum.locTypes.W:
                x = "2px";
                y = (scTop + ((fullHeight - (height + titleBarHeight)) / 2)) + "px"
                break;
            case _TF.APP.Enum.locTypes.CEN:
                x = (scLeft + ((fullWidth - width) / 2)) + (10) + "px";
                y = (scTop + ((fullHeight - (height + titleBarHeight)) / 2)) + "px"
                break;
            default: break;
        }
        return { x: x, y: y };
    };


    _TF.Controls.Dialog.postBackbPrevDlg = function(eventTarget, eventArg) {
        var el = xGetElementById("mdlDlgFrame_" + (_TF.Controls.Dialog.Iteration - 1));
        if (el != null) {
            top.frames["mdlDlgFrame_" + (_TF.Controls.Dialog.Iteration - 1)].__doPostBack(eventTarget, eventArg);
        }
    };

    _TF.Controls.Dialog.refreshPrevDlg = function(dlgId) {
        var el;
        if (dlgId != null) {
            el = _gE(dlgId);
        }
        else {
            el = xGetElementById("mdlDlgFrame_" + (_TF.Controls.Dialog.Iteration - 1));
        }

        el.src = el.src;
        return;
    };

    _TF.Controls.Dialog.getPrevDlg = function(dlgId) {
        var el;
        if (dlgId != null) {
            el = top.frames[dlgId].document.parentWindow;
        }
        else {
            el = top.frames["mdlDlgFrame_" + (_TF.Controls.Dialog.Iteration - 1)].document.parentWindow;
        }

        return el;
    };

